package com.titan.server.web.servlet;

import com.titan.server.web.impl.WebHelper;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import java.io.UnsupportedEncodingException;
import java.io.IOException;

/*********************************
 * Date: 2007-10-9
 * Time: 19:06:38
 * Description:
 *********************************/
public class TitanDispatcherServlet extends HttpServlet {

	private static final long serialVersionUID = 490104697474185624L;

	public void doGet(HttpServletRequest request, HttpServletResponse response) {
        this.doPost(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) {
        this.setCharacterEncoding(request, "UTF-8");

        String next = "";

        try {
            WebHelper webHelper = new WebHelper(request, response);
            next = webHelper.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }

        this.dispatcher(request, response, next);
    }

    private void setCharacterEncoding(HttpServletRequest request, String str) {
        try {
            request.setCharacterEncoding(str);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

    private void dispatcher(HttpServletRequest request, HttpServletResponse response, String url) {
        try {
            RequestDispatcher dispatcher = request.getRequestDispatcher(url);
            dispatcher.forward(request, response);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
